package JAVAcollectionsanddatastructures.MapandSet;

import java.util.*;

public class TestDemo1 {

    //给你50个数据，统计每个数据出现的次数
    public static Map<Integer, Integer> func(int[] array) {
        Map<Integer, Integer> map = new HashMap<>();
        //判断array中的元素是否在map中，如果在就在原来的value值上+1，不在就是1.
        for (int x : array) {
            if (map.containsKey(x)) {
                int value = map.get(x);
                map.put(x, value + 1);
            } else {
                map.put(x, 1);
            }
//            if (map.get(x) == null){
//                map.put(x,1);
//            }else{
//                int value = map.get(x);//获得存在元素的次数
//                map.put(x,value+1);
//            }
        }
        return map;
    }

    //将50个数据去重
    public static Set<Integer> func2(int[] array) {
        Set<Integer> set = new HashSet<>();
        for (int x : array) {
            set.add(x);
        }
        return set;
    }

    //找出第一个重复的元素
    public static int func3(int[] array) {
        Set<Integer> set = new HashSet<>();
        for (int x : array) {
            if (set.contains(x)) {
                return x;
            } else {
                set.add(x);
            }
        }
        return -1;
    }

    public static void main1(String[] args) {
        int[] array = new int[50];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100);//产生的随机数在1000内
        }
        Map<Integer, Integer> map = func(array);
        System.out.println(map);
        System.out.println("===========================");

        Set<Integer> set = func2(array);
        System.out.println(set);
        System.out.println("===========================");

        int ret = func3(array);
        System.out.println(ret);
    }


    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

    //复制带随机指针的链表
    public Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        Node cur = head;
        //第一次遍历，遍历一个老节点就创建一个新节点
        while (cur != null) {
            Node node = new Node(cur.val);
            map.put(cur, node);
            cur = cur.next;
        }
        cur = head;
        //第二次遍历，根据hashmap里存储的新老节点的关系来修改新节点的next和random
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }


    //查看石头中有多少宝石
    public int numJewelsInStones(String jewels, String stones) {
        Set<Character> set = new HashSet<>();
        for (Character ch : jewels.toCharArray()) {
            set.add(ch);
        }
        int count = 0;
        for (Character ch : stones.toCharArray()) {
            if (set.contains(ch)) {
                count++;
            }
        }
        return count;
    }

//        Set<Character> set = new HashSet<>();
//        char[] ret = jewels.toCharArray();
//        //for(Character ch : jewels.toCharArray())
//        for (int i = 0; i < ret.length; i++) {
//            set.add(ret[i]);
//        }
//        int count = 0;
//        char[] ret1 = stones.toCharArray();
//        for (int i = 0; i < ret1.length; i++) {
//            if (set.contains(ret[i]));
//            count++;
//        }
//        return count;
//    }


    public static void func(String strExce, String strActual) {
        Set<Character> set = new HashSet<>();
        for (char x : strActual.toUpperCase().toCharArray()) {//变成大写再转成数组
            set.add(x);
        }
        Set<Character> set1 = new HashSet<>();
        for (char ch : strExce.toUpperCase().toCharArray()) {
            if (!set.contains(ch) && !set1.contains(ch)) {
                set1.add(ch);
            }
        }
        System.out.println(set1);
    }

    public static void main2(String[] args) {
        func("7_this_is_a_test_l_p", "_hs_s_a_es");
    }


    //求前k个出现次数最多的单词:   **************没搞懂***************
    public static List<String> topKFrequent(String[] words, int k) {
        HashMap<String, Integer> map = new HashMap<>();
        //1.统计每个单词出现的个数
        for (String s : words) {
            if (map.get(s) == null) {
                map.put(s, 1);
            } else {
                int value = map.get(s);
                map.put(s, value + 1);
            }
        }
        //2.建立一个大小为k的小根堆
        PriorityQueue<Map.Entry<String, Integer>> minheap = new PriorityQueue<>(k, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        });
        //3.遍历map
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (minheap.size() < k) {
                minheap.offer(entry);
            } else {
                Map.Entry<String, Integer> top = minheap.peek();
                if (top.getValue().compareTo(entry.getValue()) > 0){
                    minheap.poll();
                    minheap.offer(entry);
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String[] words = {"hello","tc","tc","hello","ghj","fdbgf","tc"};
        topKFrequent(words,2);
    }

}
